package com.tjec.project.mdmSynchronous.service.impl;

import com.tjec.common.utils.StringUtils;
import com.tjec.project.mdmSynchronous.dao.BpmRejectCommentDao;
import com.tjec.project.mdmSynchronous.domain.RejectComment;
import com.tjec.project.mdmSynchronous.service.IBpmRejectCommentService;
import com.tjec.project.system.domain.SysRejectOpinion;
import com.tjec.project.system.mapper.SysRejectOpinionMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: VillWang
 * @Date: 2020/5/21 17:06
 */
@Service
public class BpmRejectCommentServiceImpl implements IBpmRejectCommentService {

	@Autowired
	private BpmRejectCommentDao bpmRejectCommentDao;
	
	@Autowired
	private SysRejectOpinionMapper sysRejectOpinionMapper;
	
	@Override
	public List<Map<String, Object>> getRejectCommentList(RejectComment rejectComment) {
		return bpmRejectCommentDao.getRejectCommentList(rejectComment);
	}

	@Override
	public List<Map<String, Object>> getRejectCommentCounts(RejectComment rejectComment) {
		return bpmRejectCommentDao.getRejectCommentCounts(rejectComment);
	}

	@Transactional
	@Override
	public int syncRejectList() {
		int res = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//查询ec驳回意见表最大的时间戳
		SysRejectOpinion ecReject = sysRejectOpinionMapper.selectMaxCreateTime();
		List<Map<String, Object>> bpmReject = new ArrayList<Map<String, Object>>();
		if(ecReject!=null && ecReject.getCreateTime()!=null) {
			//查询符合条件的最新驳回数据
			bpmReject = bpmRejectCommentDao.selectRejectComment(sdf.format(ecReject.getCreateTime()));
		}else {
			bpmReject = bpmRejectCommentDao.selectRejectComment("");
		}
		
		String maxTime = "";
		if(bpmReject!=null && bpmReject.size()>0) {
			maxTime = bpmReject.get(0).get("rejectTime").toString();
			//插入新驳回数据
			res = sysRejectOpinionMapper.insertSysRejectOpinions(bpmReject);
			if(StringUtils.isNotBlank(maxTime)) {
				SysRejectOpinion sysRejectOpinion = new SysRejectOpinion();
				try {
					sysRejectOpinion.setCreateTime(sdf.parse(maxTime));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				sysRejectOpinionMapper.updateAllSysRejectOpinion(sysRejectOpinion);
			}
		}
		
		return res;
	}
   

}
